package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProductBean;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("supplier")
@Api(tags = "供应商控制器")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    @RequestMapping("addByExcel")
    @ApiOperation("按文件批量上传供应商")
    @ApiImplicitParam(name="excelFile",value = "excel文件流",required = true)
    @RequiresPermissions("supplier:*")
    public String getExcel(@RequestParam("excelFile") MultipartFile mf) throws Exception {
        ImportParams iparam = new ImportParams();
        //标题栏占的行数
        iparam.setTitleRows(1);
        //表头占的行数
        iparam.setHeadRows(1);
        //设置选项卡
        iparam.setSheetNum(1);
        //读取上传excel文件数据，构建为实体List集合
        List<SupplierBean> list = ExcelImportUtil.importExcel(mf.getInputStream(), SupplierBean.class, iparam);
        return  supplierService.addSupplierList(list);
    }
    @ApiOperation(value = "动态分页查询供应商信息", notes = "按页码，名称，经营范围查询供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNO",value = "页码",required = true),
            @ApiImplicitParam(name="name",value = "名称"),
            @ApiImplicitParam(name="scope",value = "经营范围")
    })
    @RequestMapping("cutByItem")
    @RequiresPermissions("supplier:*")
    public IPage<SupplierBean> cutByItem(Integer pageNO,String name,String scope){

      return  supplierService.cutByItem(pageNO,name,scope);
    }
    @ApiOperation("按编号查询供应商信息")
    @ApiImplicitParam(name="id",value = "编号")
    @RequestMapping("findById")
    @RequiresPermissions("supplier:*")
    public SupplierBean findById(Integer id){
        return supplierService.findById(id);
    }
    @ApiOperation(value = "修改供应商信息",notes = "按编号修改供应商负责人，负责人电话")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "编号",required = true),
            @ApiImplicitParam(name="person",value = "负责人",required = true),
            @ApiImplicitParam(name="phone",value = "负责人电话",required = true)
    })
    @RequestMapping("update")
    @RequiresPermissions("supplier:*")
    public String update(Integer id,String person,String phone){
        supplierService.update(id,person,phone);
        return "ok";
    }

    @ApiOperation("添加供应商信息")
    @RequestMapping("add")
    @RequiresPermissions("supplier:*")
    public String add(SupplierBean supplier){
        supplierService.add(supplier);
        return "ok";
    }
    @ApiOperation("查找所有供应商")
    @RequestMapping("findAll")
    @RequiresPermissions("supplier:*")
    public List<SupplierBean> findAll(){
       return supplierService.findAll();
    }
}
